from odoo import models, fields, api


class Course(models.Model):
    _name = 'openacademy.course'
    _description = fields.Text(string="课程")

    name = fields.Char(string="课程名称", required=True)
    description = fields.Text(string="课程说明")
    responsible_id = fields.Many2one("res.users", ondelete="set null", string="负责人", index=True)
    session_ids = fields.One2many("openacademy.session", "course_id", string="开课情况")
    level = fields.Selection([
        (1, "简单"),
        (2, "中等"),
        (3, "困难"),
    ], string="难易程度")

    session_count = fields.Integer(string="开课数", compute="_compute_session_count")


    @api.depends("session_ids")
    def _compute_session_count(self):
        for course in self:
            course.session_count = len(course.session_ids)

    
